﻿#include "mainwindow.h"

#include <QKeyEvent>
#include <QMouseEvent>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("QKeyEvent & QMouseEvent Example");
    resize(600, 400);
}

MainWindow::~MainWindow()
{
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    // 判断按下的键并进行相应的处理
    switch (event->key()) {
    case Qt::Key_W:
        qDebug() << "Key W pressed";
        break;
    case Qt::Key_S:
        qDebug() << "Key S pressed";
        break;
    case Qt::Key_A:
        qDebug() << "Key A pressed";
        break;
    case Qt::Key_D:
        qDebug() << "Key D pressed";
        break;
    default:
        // 调用父类的默认处理
        QMainWindow::keyPressEvent(event);
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        qDebug() << "Left mouse button pressed at" << event->pos();
    } else if (event->button() == Qt::RightButton) {
        qDebug() << "Right mouse button pressed at" << event->pos();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        qDebug() << "Left mouse button released at" << event->pos();
    } else if (event->button() == Qt::RightButton) {
        qDebug() << "Right mouse button released at" << event->pos();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    qDebug() << "Mouse moved to" << event->pos();
}

